package com.rssfeed.database;


import java.util.ArrayList;
import com.rssfeed.model.RSSFeed;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBController {
	
	private SQLiteDatabase database;
	private DBSqliteHelper dbHelper;
	private String[] allColumns ={dbHelper.mFeedId,dbHelper.mTitle,dbHelper.mDescription,dbHelper.mLink,dbHelper.mImageLink,dbHelper.mPubDate};
	
	public DBController(Context context){
		dbHelper = new DBSqliteHelper(context);
	}
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	
	public long addData( String  id, String til,String des,String link,String date,String imgLink,String mp3Link){
		ContentValues cv=new ContentValues();
		open();
		cv.put(DBSqliteHelper.mFeedId, id);
		cv.put(DBSqliteHelper.mTitle, til);
		cv.put(DBSqliteHelper.mDescription, des);
		cv.put(DBSqliteHelper.mLink, link);
		cv.put(DBSqliteHelper.mPubDate, date);
		cv.put(DBSqliteHelper.mImageLink, imgLink);
		cv.put(DBSqliteHelper.mMp3Link, mp3Link);
		long insertId =  database.insert(DBSqliteHelper.TABLE_DATA, null,cv);
		close();
		return  insertId;
		
	}
		
	public ArrayList<RSSFeed> getdata(){
		//String[] columns=new String[]{DBSqliteHelper.mTitle,DBSqliteHelper.mDescription,DBSqliteHelper.mLink,DBSqliteHelper.mPubDate};
		Cursor c=database.query(DBSqliteHelper.TABLE_DATA, allColumns, null, null,null, null, null);
		
		ArrayList<RSSFeed> listRss=new ArrayList<RSSFeed>();
		
		int iFeedId= c.getColumnIndex(DBSqliteHelper.mFeedId);
		int ititle= c.getColumnIndex(DBSqliteHelper.mTitle);
		int ilink= c.getColumnIndex(DBSqliteHelper.mLink);
		int ides= c.getColumnIndex(DBSqliteHelper.mDescription);		
		int idate= c.getColumnIndex(DBSqliteHelper.mPubDate);
		int iimage =  c.getColumnIndex(DBSqliteHelper.mImageLink);
		//int imp3 = c.getColumnIndex(DBSqliteHelper.mMp3Link);
		
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			RSSFeed rfs=new RSSFeed();
			rfs.setFeedId(c.getString(iFeedId));
			rfs.setTitle(c.getString(ititle));
			rfs.setDescription(c.getString(ides));
			rfs.setLink(c.getString(ilink));
			rfs.setPubDate(c.getString(idate));
			rfs.setImgLink(c.getString(iimage));
			//rfs.setMp3Link(c.getString(imp3));
			listRss.add(rfs);
		}
		c.close();
		return listRss;
	}
	
	/**
	 * @author Kenzo
	 * @param feedId
	 * @date 23-05
	 * @return feed Id exists or not
	 */
	
	public boolean checkID(String feedId){
		open();
		Cursor c = database.rawQuery("select * from "+DBSqliteHelper.TABLE_DATA+" where "+ DBSqliteHelper.mFeedId + " = ?", new String[] {feedId});
		if(c!=null && c.getCount()>0){
			return true;
		}
		c.close();
		close();
		return false;
	}
	/**
	 * @author Kenzo
	 * @date 23-05
	 * @param mp3Link
	 * @return mp3Link exist or not
	 */
	
	public boolean checkMp3Link(String mp3Link){
		open();
		Cursor c = database.rawQuery("select * from "+DBSqliteHelper.TABLE_DATA+" where "+ DBSqliteHelper.mMp3Link + " = ?", new String[] {mp3Link});
		if(c!=null && c.getCount()>0){
			return true;
		}
		c.close();
		close();
		return false;
	}
	
	public long updateMp3Link(String feedId, String mp3Link){
		open();
		ContentValues cv = new ContentValues();
		cv.put(DBSqliteHelper.mMp3Link, mp3Link);
		long result = database.update(DBSqliteHelper.TABLE_DATA, cv,DBSqliteHelper.mFeedId + "='" + feedId + "'" , null);
		close();
		return result;
	}
	
}
